<template>
  <el-dialog title="分配人员" :visible.sync="dialogVisible">
    <el-form>
      <tree-transfer ref='userSelect' title="人员选择" v-model="formData.userIds" :data="treeData" :selected="selected" @node-click="handleNodeClick" />
    </el-form>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import TreeTransfer from '@/pages/components/treeTransfer'
  import request from '@/plugin/axios'
  import postRequest from '@/libs/axiosSupport'
  import * as service from "@/api/crud.base";
  export default {
    components: {
      TreeTransfer
    },
    data() {
      return {
        dialogVisible: false,
        selected: [],
        formData: {},
        treeData: []
      }
    },
    methods: {
      save() {
        this.loading = true;
        postRequest(
          '/roles/assignUsers',
          this.formData
        ).then(res => {
          this.$message({
            type: 'success',
            message: '分配成功'
          });
          this.loading = false;
          this.dialogVisible = false;
        })
      },
      reset(){
        this.formData = {};
        this.selected = [];
        this.treeData = [];
      },
      open(row) {
        this.dialogVisible = true;
        this.reset();
        this.queryUsersByRoleId(row);
        service.list('/orgs').then(res => {
          this.treeData = res;
        });
        Object.assign(this.formData, {
          roleId: row.id
        })
      },
      handleNodeClick(data){
         let params={};
        if(data.pid){
          params={orgId:data.id}
        }
        service.list('/users',params).then(res => {
          this.$refs.userSelect.setTransData(res);
        })
      },
      queryUsersByRoleId(row) {
        request({
          url: '/users/queryUsersByRoleId',
          method: 'get',
          params: {
            'roleId': row.id
          }
        }).then(res => {
          if (res.length > 0) {
            this.selected = res;
          }
        })
      }
    }
  }
</script>
